# -*- coding: utf-8 -*-
import scrapy
from chinahr.items import JobInfoItem, ComInfoItem
from scrapy.loader import ItemLoader
from scrapyluke.processors import *

__author__ = 'yanyan'


class GuoLaiRenSpider(scrapy.Spider):
    name = 'guolairen'
    allowed_domains = ['guolairen.com']
    start_urls = ['http://www.guolairen.com/zhaopin/zhaopin-list.php']

    def parse(self, response):
        total_page = ''.join(response.xpath('//div[@class="page link_bk"]/a[last()]/text()').re(u'\d+\/(\d+)页'))
        urls = ['%s?page=%s' % (response.url, str(page)) for page in range(1, int(total_page)+1)]
        for url in urls:
            yield scrapy.Request(url, callback=self.parse_urls)

    def parse_urls(self, response):
        job_urls = response.xpath('//div[@class="jobname striking"]/strong/a/@href').extract()
        com_urls = response.xpath('//div[@class="com striking"]/a/@href').extract()
        for url in job_urls:
            yield scrapy.Request('http://www.guolairen.com%s' % url, callback=self.parse_job_info)
        for url in com_urls:
            yield scrapy.Request('http://www.guolairen.com%s&act=profile' % url, callback=self.parse_com_info)

    def parse_job_info(self, response):
        loader = ItemLoader(item=JobInfoItem(), response=response)
        loader.add_value('url', value=response.url)
        loader.add_xpath('job_name', u'//div[@class="company-head fn-clear"]/h1/text()')
        loader.add_xpath('job_nature', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="职位类型："]/../text()')
        loader.add_xpath('job_salary', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="薪资待遇："]/../text()')
        loader.add_xpath('job_recruNums', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="招聘人数："]/../text()')
        loader.add_xpath('job_location', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="工作地区："]/../text()')
        loader.add_xpath('job_experience', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="工作经验："]/../text()')
        loader.add_xpath('job_update', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="更新日期："]/../text()')
        loader.add_xpath('job_miniEdu', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="学历要求："]/../text()')
        loader.add_xpath('job_reqSex', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="性别要求："]/../text()')
        loader.add_xpath('job_detail', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="截止日期："]/..', RemoveTagsL(), ReplaceBlank(''), JoinL(''))
        loader.add_xpath('job_desc', u'//div[@class="pos-description fn-clear"]/text()', RemoveTagsL(), ReplaceBlank(''), JoinL())
        return loader.load_item()

    def parse_com_info(self, response):
        loader = ItemLoader(item=ComInfoItem(), response=response)
        loader.add_value('url', value=response.url)
        loader.add_xpath('com_name', u'//div[@class="company-head fn-clear"]/h1/text()')
        loader.add_xpath('com_nature', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="企业性质："]/../text()')
        loader.add_xpath('com_industry', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="所属行业："]/../text()')
        loader.add_xpath('com_size', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="公司规模："]/../text()')
        loader.add_xpath('com_detail', u'//ul[@class="pos-attribute fn-clear"]/li/span[text()="所在地区："]/..', RemoveTagsL(), ReplaceBlank(''), JoinL(''))
        loader.add_xpath('com_intro', u'//div[@class="pos-description fn-clear"]/text()', RemoveTagsL(), ReplaceBlank(''), JoinL())
        return loader.load_item()
